<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Aftermarket as AftermarketModel;
use addons\qingdongams\model\AftermarketType;
use addons\qingdongams\model\Comment;
use addons\qingdongams\model\AdminConfig;


/**
 * 知识库
 */
class WebAftermarket extends WebIndexApi
{
    protected $noNeedLogin = ['user_config'];
    protected $noNeedRight = [];
    /**
     * @var \addons\qingdongams\model\Aftermarket
     */
    protected $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new AftermarketModel();

    }

    // 获取分类
    public function typeList(){
        $pid = input('pid',0);
        $where['pid'] = $pid;
        $model = new AftermarketType();
        $list = collection($model->order('id desc')->where($where)->field('id,name,pid')->select())->toArray();
        $this->success('',$list);
    }

    //文章列表
    public function articleList()
    {
        $limit = input("limit/d", 10);
        $params = $this->request->post();
        $where = [];
        if(isset($params['type_ids']) && $params['type_ids']){
            $where['type_ids'] = ['like','%,'.$params['type_ids'].',%'];
        }
        if(isset($params['name']) && $params['name']){
            $where['title'] = ['like','%'.$params['name'].'%'];
        }
        $comments = $this->model->where($where)->paginate($limit);
        $this->success('请求成功', $comments);
    }

    public function detailArt(){
        $id = input('id');
        if(!$id){
            $this->error('文章id呢？');
        }
        $id = input("id");
        if(!isset($id) || !$id){
            $this->error('参数缺失');
        }
        // 文章详情
        $detail = AftermarketModel::where('id',$id)->field('id,create_staff_id,product_ids,type_ids,title,content,img,desc,method,click_number,comment_num,createtime')->find();
        // 详情
        $comments = Comment::where([
            'relation_type' => 'article',
            'relation_id' => $id,
            'status' => 1
        ])->field('id,staff_id,content,createtime')->with(['staff'])->select();
        $detail['comment'] = $comments;
        $this->success('请求成功', $detail);
    }

    /**
     * 文章浏览次数
     * @throws \think\Exception
     */
    public function viewArticle(){
        $id = input('id');
        if(!$id){
            $this->error('文章id呢？');
        }
        $articleModel = new AftermarketModel();
        $row = $articleModel->get($id);
        if(!$row){
            $this->error('文章不存在');
        }
        if($articleModel->where(['id'=>$id])->setInc('click_number')){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    //客户端配置
    public function user_config(){
        $data = AdminConfig::where(array('type'=>'user'))->column('field,value');
        $data['user_name'] = $data['user_name'] ??'';
        $data['user_phone'] = $data['user_phone']??'';
        $data['user_email'] = $data['user_email'] ?? '';
        $data['user_logo'] = isset($data['user_logo'])? cdnurl($data['user_logo'],true) : '';
        $data['user_address'] = $data['user_address'] ?? '';
        $data['user_lat'] = $data['user_lat'] ?? '';
        $data['user_lag'] = $data['user_lag'] ??'';
        $this->success('',$data);
    }
}
